/**
 * ProjectName:  mall-service
 * FileName:  AUserServiceImpl.java
 * PackageName:  com.blx..service.impl
 * Copyright (c) 2013, CYOU All Rights Reserved.
*/
package com.blx.service.impl;

import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.stereotype.Service;

import com.blx.dao.AUserDao;
import com.blx.entity.AUser;
import com.blx.entity.QUser;
import com.blx.model.constants.ResponseConstants;
import com.blx.model.constants.WebConstants;
import com.blx.model.request.RegisterRequest;
import com.blx.model.response.RegisterResponse;
import com.blx.service.AUserService;
import com.blx.utils.pagination.Pagination;

/**
 * ClassName:  AUserImpl. <br/>
 * Description: AUserService接口实现 <br/>
 * @author wangwl
 * 2013-3-4 下午9:02:20 <br/>
 * @version 1.0
 */
@Service
public class AUserServiceImpl implements AUserService {

	private final Logger logger = LoggerFactory.getLogger(getClass());

	@Autowired
	private AUserDao aUserDao;

	/**
	 * 保存AUser实体
	 */
	public void saveAUser(AUser aUser) {
		aUserDao.saveAUser(aUser);
	}
	/**
	 * 更新AUser实体
	 */
	public void updateAUser(AUser aUser) {
		aUserDao.updateAUser(aUser);
	}
	/**
	 * 根据id删除AUser
	 */
	public void deleteAUserById(Integer aUserId) {
		aUserDao.deleteAUserById(aUserId);
	}
	/**
	 * 根据id获取AUser
	 */
	public AUser getAUser(Integer aUserId) {
		return aUserDao.getAUserById(aUserId);
	}
	/**
	 * 分页查询AUser
	 */
	public Pagination<AUser> findPageAUserAll(int pageNumber, int pageSize) {
		return aUserDao.findPageAUserAll(pageNumber, pageSize);
	}
	@Override
	public AUser getAUserEntity(Map<String,Object> map) {
		return aUserDao.getAUserEntity(map);
	}
	@Override
	public RegisterResponse userRegister(RegisterRequest request) {
		RegisterResponse response = new RegisterResponse();
		try {
			/**
			 * 1、先验证两次密码是否相同
			 *  若不同，直接返回失败
			 * 2、插入数据库如果成功则成功，失败则失败返回
			 */
			boolean flag = passportCheck(request);
			if(!flag){
				response.setResponseCode(ResponseConstants.FAIL_SAME_PWD);
				response.setErrorMsg("passwords are not the same");
				return response;
			}
			AUser auser = buildRegisterQuer(request);
			if(auser == null){
				response.setResponseCode(ResponseConstants.FAIL_PASSPORT_EXISTS);
				response.setErrorMsg("parameter is incorrect");
				return response;
			}
			aUserDao.saveAUser(auser);
		}catch(DuplicateKeyException de){
			response.setResponseCode(ResponseConstants.FAIL_PARAM_ERROR);
			response.setErrorMsg("passport has exists");
			return response;
		}catch (Throwable t) {
			logger.error("",t);
			t.printStackTrace();
			response.setResponseCode(ResponseConstants.ERROR);
			response.setErrorMsg("server error");
			return response;
		}
		return null;
	}
	private AUser buildRegisterQuer(RegisterRequest request) {
		if(request != null){
			boolean flag = isPassport(request.getPassport());
			if(!flag){
				return null;
			}
			AUser auser = new AUser();
			auser.setPassport(request.getPassport());
			auser.setPassword(request.getPassword());
			if(request.getTeltphone() != null && (!request.getTeltphone().equals(""))){
				auser.setTelephone(request.getTeltphone());
			}
			return auser;
		}
		return null;
	}
	private boolean passportCheck(RegisterRequest request) {
		if(request != null && request.getPassword() != null && request.getConfirmPwd() != null && (!request.getPassword().equals("")) && (!request.getConfirmPwd().equals(""))){
			String password = request.getPassword();
			String conPwd = request.getConfirmPwd();
			if(password.equals(conPwd)){
				return true;
			}
			return false;
		}
		return false;
	}
	/**
	 * 判断是否是邮箱格式
	 */
	private boolean isPassport(String passport){
		Pattern pattern = Pattern.compile(WebConstants.REG_EMAIL);
		Matcher m = pattern.matcher(passport);
		return m.find();
	}
	private boolean isUserName(String userName){
		Pattern pattern = Pattern.compile(WebConstants.REG_NIKE_NAME);
		Matcher m = pattern.matcher(userName);
		return m.find();
	}

}

